﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Cms.BLL.WepPart
{
    public class TextPart : IWebpart
    {
        private Model.Sys_Webpart Wpt = null;
        private Dictionary<string, string> Require = new Dictionary<string, string>();
        public TextPart(Model.Sys_Webpart wpt)
        {
            Wpt = wpt;
        }
        public TextPart(Model.Sys_Webpart wpt, bool allowclose, bool allowmin, bool allowmax, bool allowmove, string width, string height)
        {
            Wpt = wpt;
            Wpt.Allow_Close = allowclose;
            Wpt.Allow_Min = allowmin;
            Wpt.Allow_Max = allowmax;
            Wpt.Allow_Move = allowmove;
            if (!String.IsNullOrEmpty(width) && EntLib.TypeParse.IsNumeric(width))
                Wpt.Width = EntLib.TypeParse.StrToInt(width, 0);
            if (!String.IsNullOrEmpty(height) && EntLib.TypeParse.IsNumeric(height))
                wpt.Height = EntLib.TypeParse.StrToInt(height, 0);
        }
        #region IWebpart Members

        public string BuilderWpt()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<div class='webpart' id='{0}'>", Wpt.Id);
            sb.AppendFormat("<div class='webpart-title'><h2 class='text'>{0}</h2><div class='btn'>", Wpt.Title);
            if (Wpt.Allow_Min)
                sb.AppendFormat("<span class='title-icon min'>＿</span>");
            if (Wpt.Allow_Max)
                sb.AppendFormat("<span class='title-icon max'>□</span>");
            if (Wpt.Allow_Close)
                sb.AppendFormat("<span class='title-icon close'>×</span>");

            sb.AppendFormat("</div><b class='clear'></b></div>");
            sb.AppendFormat("<div class='webpart-content'");
            sb.AppendLine(" style='");
            if ((Wpt.Width ?? 0) > 0)
                sb.AppendFormat("width:{0}px;", Wpt.Width);
            if ((Wpt.Height ?? 0) > 0)
                sb.AppendFormat("height:{0}px;", Wpt.Height);
            sb.Append("'");
            sb.AppendLine(">");
            sb.AppendLine(Builder());
            sb.AppendFormat("</div><b class='clear'></b>");
            sb.AppendFormat("</div>");
            return sb.ToString();
        }

        private string BuilderRss(string rssContent)
        {
            StringBuilder result = new StringBuilder();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(rssContent);
            XmlNodeList Channels = xmlDoc.SelectNodes("rss/channel");
            foreach (XmlNode xnChanel in Channels)
            {
                result.AppendLine("<ul class='channel'>");
                result.AppendFormat("<div class='channel-title'>{0}</div>", EntLib.Utiltiy.GetXmlChildText(xnChanel, "title"));
                foreach (XmlNode xnItem in xnChanel.SelectNodes("//item"))
                {
                    result.AppendFormat("<li class='item'>");
                    string image = EntLib.Utiltiy.GetXmlChildText(xnItem, "image");
                    if (!String.IsNullOrEmpty(image))
                    {
                        result.AppendFormat("<img class='item-img' alt='{0}' src='{1}'/>", EntLib.Utiltiy.GetXmlChildText(xnItem, "image/title"), EntLib.Utiltiy.GetXmlChildText(xnItem, "image/link"));
                    }
                    result.AppendFormat("<a class='item-link' href='{0}'>{1}</a>", EntLib.Utiltiy.GetXmlChildText(xnItem, "link"), EntLib.Utiltiy.GetXmlChildText(xnItem, "title"));
                    string description = EntLib.Utiltiy.GetXmlChildText(xnItem, "description");
                    if (!String.IsNullOrEmpty(description))
                    {
                        result.AppendFormat("<div class='item-description'>{0}</div>", description);
                    }
                    result.AppendLine("<ul class='other'>");
                    string tempstr = EntLib.Utiltiy.GetXmlChildText(xnItem, "pubDate");
                    if (!String.IsNullOrEmpty(tempstr))
                        result.AppendFormat("<li class='pubDate'>{0}</li>", tempstr);

                    tempstr = EntLib.Utiltiy.GetXmlChildText(xnItem, "lastBuildDate");
                    if (!String.IsNullOrEmpty(tempstr))
                        result.AppendFormat("<li class='lastBuildDate'>{0}</li>", tempstr);
                    tempstr = EntLib.Utiltiy.GetXmlChildText(xnItem, "category");
                    if (!String.IsNullOrEmpty(tempstr))
                        result.AppendFormat("<li class='category'>{0}</li>", EntLib.Utiltiy.GetXmlChildText(xnItem, "category"));
                    tempstr = EntLib.Utiltiy.GetXmlChildText(xnItem, "comments");
                    if (!String.IsNullOrEmpty(tempstr))
                        result.AppendFormat("<li class='comments'>{0}</li>", EntLib.Utiltiy.GetXmlChildText(xnItem, "comments"));
                    tempstr = EntLib.Utiltiy.GetXmlChildText(xnItem, "sourse");
                    if (!String.IsNullOrEmpty(tempstr))
                        result.AppendFormat("<li class='sourse'>{0}</li>", EntLib.Utiltiy.GetXmlChildText(xnItem, "sourse"));
                    result.AppendLine("</ul>");
                    result.AppendFormat("</li>");
                }
                result.AppendLine("</ul>");
            }
            return result.ToString();
        }

        public string Builder()
        {
            string result = "";
            IDataSource wptDataSource=null;
            switch (Wpt.Scripttype)
            {
                case Cms.Model.DataSouceType.Text:
                    result = Wpt.Template;
                    break;
                case Cms.Model.DataSouceType.RssLink:
                    SetRequire("style", "rss");
                    /*string content = EntLib.Utiltiy.GetHttpData(Wpt.Serverscript);
                    result = BuilderRss(content);*/
                    Model.RssBody rssBody=RssBuilder.GetRssItem(new Model.RssSource(Model.RssSourceType.Url,Wpt.Serverscript),true);
                    wptDataSource = new RssDataSource(rssBody);
                    break;
                case  Model.DataSouceType.Sql :
                    wptDataSource = new SqlDataSource(Wpt.Serverscript);
                    break;
                default:
                    result = Wpt.Template;
                    break;
            }
            if (wptDataSource != null) {
                result = wptDataSource.Builder("");
            }
            SetRequire("style", "webpart");
            SetRequire("script", "jquery");
            SetRequire("script", "webpart");
            return result;
        }
        public void SetRequire(string type, string value)
        {
            if (!this.Require.ContainsKey(type))
                this.Require.Add(type, value);
            else
            {
                string v = this.Require[type];
                if (v.IndexOf(value) < 1)
                    v = v + "," + value;
                this.Require[type] = v;
            }
        }
        public Dictionary<string, string> GetRequire()
        {
            return Require;
        }

        #endregion
    }
    public class UrlPart { }
    public class FormPart { }

}
